// 例子7


function p1() {
    console.log('【1】');

    // ①
    var promise1 = new Promise(function(resolve, reject) {
        setTimeout(resolve, 1000, 1);
    })
    var promise1_1 = promise1.then(function(value) {
        return value + 5;
    });
    var promise1_2 = promise1_1.then(function(value) {
        console.log(value);
        console.log(promise1_1);
        p2();
    })
}

function p2() {
    console.log('###########################################');
    console.log('【2】');

    // ②
    var promise2 = new Promise(function(resolve, reject) {
        setTimeout(resolve, 1000, 2);
    })
    var promise2_1 = promise2.then(value => {
        throw '出现错误';
    });
    var promise2_2 = promise2_1.then(function(value) {
        console.log(value);
    }, function(err) {
        console.log(err);
        console.log(promise2_1);
        p3();
    });
}

function p3() {
    console.log('###########################################');
    console.log('【3】');

    // ③
    var promise3 = new Promise(function(resolve, reject) {
        setTimeout(resolve, 1000, 3);
    })
    var promise3_1 = promise3.then(function(value) {
        console.log(promise3);
        return promise3;
    });
    var promise3_2 = promise3_1.then(function(value) {
        console.log(value);
        console.log(promise3_1);
        p4();
    });
}

function p4() {
    console.log('###########################################');
    console.log('【4】');

    // ④
    var promise4 = new Promise(function(resolve, reject) {
        setTimeout(reject, 1000, '出现错误');
    })
    var promise4_1 = promise4.then(function(value) {
        console.log(value)
    }, function(err) {
        console.log(promise4);
        return promise4;
    });
    var promise4_2 = promise4_1.then(function(value) {
        console.log(value);
    }, function(err) {
        console.log(err);
        console.log(promise4_1);
        p5();
    });
}

function p5() {
    console.log('###########################################');
    console.log('【5】');

    // ⑤
    var promise5_1_1;
    var promise5 = new Promise(function(resolve, reject) {
        setTimeout(resolve, 1000, 5);
    })
    var promise5_1 = promise5.then(function(value) {
        promise5_1_1 = new Promise(function(resolve, reject) {
                setTimeout(resolve, 5000, 10);
        })
        console.log(promise5_1_1);
        console.log(promise5_1);
        return promise5_1_1;
    });
    var promise5_2 = promise5_1.then(function(value) {
        console.log(value);
        console.log(promise5_1_1);
        console.log(promise5_1);
    });
}

p1();
